package test;

import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.io.IOException;

import javax.swing.JOptionPane;

public class ThreadClientRead extends Thread {
	ClientView clientView;
	String data = "";
//	JFrame frame = new JFrame();
//	JLabel label = new JLabel("");
	CallWindow callWindow ;

	public ThreadClientRead(ClientView clientView) {
//		frame.getContentPane().add(label);
//		frame.setSize(400, 40);
//		frame.setVisible(true);
//		frame.setLocation(0, -45);
		this.clientView = clientView;
		callWindow = new CallWindow(clientView);
	}

	@Override
	public void run() {
		while (true) {
			try {
				String data = clientView.bufferedReader.readLine();
//				System.out.println(data);
				if (data == null) {

					break;
				}if (data.equals("focs875548915464")) {
					clientView.reading();
					clientView.buttonNewMess.setEnabled(false);
					clientView.lblReadAlready.setText("Đã đọc");
				}else if (data.equals("lofo2484534498756")) {
					clientView.watting();
					clientView.buttonNewMess.setEnabled(true);
				}else


				if (data.equals("call75315987412369")) {

					callWindow.changeToCall();
					callWindow.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2);
					callWindow.setCalled(CallWindow.CALLED);
					callWindow.setVisible(true);

				} else if (data.equals("new56474456488948")) {

					System.out.println("new server");
					callWindow.changeToNewMess();
					callWindow.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2);
					callWindow.setCalled(CallWindow.NEW_MESS);
					callWindow.setVisible(true);

				} else if (data.startsWith(ChatterWindows.TEMP_VIEW_STRING)) {

					data = data.replace(ChatterWindows.TEMP_VIEW_STRING, "");
					data = MaHoa.giaiMa(clientView.key, data);
					clientView.textAreaTempView.setText(data);
					clientView.textAreaTempView.setCaretPosition(clientView.textAreaTempView.getDocument().getLength());

				}  else {
				data = MaHoa.giaiMa(clientView.key, data);
				clientView.textAreaView.setText(clientView.textAreaView.getText()
						+ "\n "+clientView.name+"("+Chatter_Util.getCurrentTime()+"): " + data);

				// scroll to end
				clientView.textAreaView.setCaretPosition(clientView.textAreaView.getDocument().getLength());
				clientView.textAreaTempView.setText("");


				if(clientView.disPlayNewMess == false || !clientView.isFocused()){
					clientView.disPlayNewMess = true;
					clientView.trayIcon.displayMessage("Chatter", "You have new message...", TrayIcon.MessageType.INFO);
					clientView.newMess(true);
				}

//				if (!clientView.an) {
//					clientView.setVisible(true);
//					clientView.textView.select(clientView.textView.getText()
//							.length(), clientView.textView.getText().length());
//				}

//				label.setText(data);


//				if (!clientView.an) {
//					clientView.setVisible(true);
//					clientView.setFocusable(true);
//				}

//				int y = 40;
//				for (y = 40; y < 90; y += 2) {
//					try {
//						Thread.sleep(15);
//						frame.setSize(400, y);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//				try {
//					Thread.sleep(1000);
//				} catch (InterruptedException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//				for (; y > 30; y -= 2) {
//					try {
//						Thread.sleep(15);
//						frame.setSize(400, y);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//
//				}
//				frame.setVisible(false);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("Server đã tắt");
				JOptionPane.showMessageDialog(null, "Server đã tắt", "Client error",
						JOptionPane.ERROR_MESSAGE);
				break;
			}
		}
		JOptionPane.showMessageDialog(null, "Server đã tắt ", "Client error",
				JOptionPane.ERROR_MESSAGE);
		System.exit(0);
	}

}
